package com.zzq.calcgame.storage;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zzq.calcgame.entity.Papers;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by android on 16/9/29.
 */

public class PapersDao {

    CCOpenHelper dbHelper;

    public PapersDao(Context context){
        dbHelper=new CCOpenHelper(context);
    }

    /**
     *
     * @param papers
     * @return -1 插入失败 否则插入值的id
     */
    public int insert(Papers papers){
        int retValue=-1;
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(CCDb.PaperTable.QUESTION_COUNT,papers.getQuestionCount());
        values.put(CCDb.PaperTable.TOTAL_SCORE,papers.getTotalScore());
        values.put(CCDb.PaperTable.WRONG_COUNT,papers.getWrongCount());
        values.put(CCDb.PaperTable.CREATE_TIME,papers.getCreateTime());
        if(db.insert(CCDb.PaperTable.TABLE_NAME,null,values)!=0){
            Cursor cursor=db.rawQuery("select last_insert_rowid() from "+ CCDb.PaperTable.TABLE_NAME,null);
            if(cursor.moveToFirst()){
                retValue=cursor.getInt(0);
            }
            if(!cursor.isClosed())
                cursor.close();
        }
            db.close();
        return retValue;
    }

    public int update(Papers papers){
        int retValue=-1;
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(CCDb.PaperTable.TOTAL_SCORE,papers.getTotalScore());
        values.put(CCDb.PaperTable.QUESTION_COUNT,papers.getQuestionCount());
        values.put(CCDb.PaperTable.WRONG_COUNT,papers.getWrongCount());
        values.put(CCDb.PaperTable.CREATE_TIME,papers.getCreateTime());
        values.put(CCDb.PaperTable.ID,papers.getId());
        retValue=db.update(CCDb.PaperTable.TABLE_NAME,values, CCDb.PaperTable.ID+" = ?",new String[]{String.valueOf(papers.getId())});
        db.close();
        return retValue;
    }

    /**
     * + ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
     + CREATE_TIME + " TEXT ,"
     + TOTAL_SCORE +" INTEGER,"
     + WRONG_COUNT +" INTEGER,"
     + QUESTION_COUNT +" INTEGER)";
     * @return
     */
    public List<Papers> getAll(){
        ArrayList<Papers> paperses=new ArrayList<>();
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from "+ CCDb.PaperTable.TABLE_NAME,null);
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            Papers item=new Papers();
            item.setId(cursor.getInt(0));
            item.setCreateTime(cursor.getLong(1));
            item.setTotalScore(cursor.getInt(2));
            item.setWrongCount(cursor.getInt(3));
            item.setQuestionCount(cursor.getInt(4));
            paperses.add(item);
        }
        if(!cursor.isClosed()) {
            cursor.close();
        }
        db.close();
        return paperses;
    }

}
